package com.xm.concurrent.base;

import com.xm.concurrent.base.util.SleepUtils;

/**
 * 4.1.5 Daemon 线程
 *
 * @author xiaoming
 * @date 2021-05-19 22:16
 */
public class Daemon {
    public static void main(String[] args) {
        Thread thread = new Thread(new DaemonRunner(), "DaemonRunner");
        /** 启动之前设置 */
        thread.setDaemon(true);
        thread.start();
    }

    static class DaemonRunner implements Runnable {

        @Override
        public void run() {
            try {
                System.out.println("Thread : " + Thread.currentThread().getName() + " is running....");
                SleepUtils.second(10);
            } finally {
                // daemon 线程不保证 finally 块执行 (虚拟机退出)
                System.out.println("DaemonThread finally run.");
            }
        }
    }
}
